﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyErp.ViewModel;
using EasyErp.Domain;

namespace EasyErp.Controllers
{
    public class TimeTableController : Controller
    {
        erpEntities db = new erpEntities();

        //
        // GET: /TimeTable/Index/1

        public ActionResult Create(int id)
        {
            List<StyleOperation> styleOperationList = db.StyleOperation.Where(x => x.StyleId == id).ToList();

            List<StyleSizes> styleSizeList = db.StyleSizes.Where(x => x.StyleId == id).ToList();

            TimeTableViewModel timeTableViewModel = new TimeTableViewModel
            {
                StyleId = id,
                StyleOperationList = styleOperationList,
                StyleSizeList = styleSizeList
            };

            return View(timeTableViewModel);
        }

        //
        // POST: /TimeTable/Index/

        [HttpPost]
        public ActionResult Create(TimeTableViewModel timeTableViewModel)
        {
            if (ModelState.IsValid)
            {
                TimeTable timeTable;

                int i = 0;

                foreach (var operation in timeTableViewModel.StyleOperationList)
                {
                    foreach (var sizes in timeTableViewModel.StyleSizeList)
                    {
                        timeTable = new TimeTable
                        {
                            StyleOperationId = operation.StyleOperationId,
                            StyleSizesId = sizes.StyleSizesId,
                            Time = timeTableViewModel.Time[i]
                        };

                        db.TimeTable.Attach(timeTable);
                        db.Entry(timeTable).State = System.Data.EntityState.Modified;

                        i++;
                    }
                }
                db.SaveChanges();
                RedirectToAction("Index", "Style");
            }

            return View(timeTableViewModel);
        }

        //
        // GET: /TimeTable/Edit/1

        public ActionResult Edit(int id)
        {
            List<StyleOperation> styleOperationList = db.StyleOperation.Where(x => x.StyleId == id).ToList();

            List<StyleSizes> styleSizeList = db.StyleSizes.Where(x => x.StyleId == id).ToList();

            List<decimal> timeList = db.TimeTable.Select(x => x.Time).ToList();

            List<int> timetableIdList = db.TimeTable.Select(x => x.TimeTableId).ToList();

            TimeTableViewModel timeTableViewModel = new TimeTableViewModel
            {
                StyleId = id,
                StyleOperationList = styleOperationList,
                StyleSizeList = styleSizeList,
                Time = timeList,
                TimeTableIdList = timetableIdList
            };

            return View(timeTableViewModel);
        }

        //
        // POST: /TimeTable/Edit

        [HttpPost]
        public ActionResult Edit(TimeTableViewModel timeTableViewModel)
        {
            if (ModelState.IsValid)
            {
                TimeTable timeTable;

                int i = 0;

                foreach (var operation in timeTableViewModel.StyleOperationList)
                {
                    foreach (var sizes in timeTableViewModel.StyleSizeList)
                    {
                        timeTable = new TimeTable
                        {
                            StyleOperationId = operation.StyleOperationId,
                            StyleSizesId = sizes.StyleSizesId,
                            Time = timeTableViewModel.Time[i],
                            TimeTableId = timeTableViewModel.TimeTableIdList[i]
                        };

                        db.Entry(timeTable).State = System.Data.EntityState.Modified;
                        
                        i++;
                    }
                }
                db.SaveChanges();
                
                return RedirectToAction("Index", "Style");
            }

            return View();
        }
    }
}
